﻿using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

namespace GameToolkit.Editor
{
    [CustomEditor(typeof(PanelClip)), CanEditMultipleObjects]
    public class PanelClipEditor : UnityEditor.Editor
    {
        SerializedProperty m_Intent;
        SerializedProperty m_UsingPanel;
        SerializedProperty m_PauseTimeline;

        GUIContent label;

        private void OnEnable()
        {
            if (label == null)
                label = new GUIContent();
            m_UsingPanel = serializedObject.FindProperty("m_UsingPanel");
            m_Intent = serializedObject.FindProperty("m_Intent");
            m_PauseTimeline = serializedObject.FindProperty("m_PauseTimeline");
        }

        private void OnDisable()
        {
            m_Intent = null;
        }

        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            var provider = SearchPanelIntentProvider.Get();
            serializedObject.Update();
            bool changed = false;
            EditorGUI.BeginChangeCheck();
            var pause = m_PauseTimeline.boolValue;
            EditorGUILayout.PropertyField(m_PauseTimeline);
            changed |= pause != m_PauseTimeline.boolValue;

            EditorGUILayout.PropertyField(m_UsingPanel);
            var multi = m_Intent.hasMultipleDifferentValues;
            var v = m_Intent.managedReferenceValue;
            var rect = EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel(m_Intent.displayName);
            var info = multi || v == null ? null : SearchPanelIntentProvider.GetNodeInfo(v.GetType());
            label.text = info == null ? "-" : info.Title;
            var drop = EditorGUILayout.DropdownButton(label, FocusType.Keyboard);
            EditorGUILayout.EndHorizontal();
            if (drop)
            {
                var width = Mathf.Max(rect.width, 120);
                if (width > 240)
                    width = 240;
                var pos = new Vector2(rect.xMax - width * 0.5f, rect.yMax + 17);
                pos = GUIUtility.GUIToScreenPoint(pos);
                provider.PerformOnce(SelectIntent);
                SearchWindow.Open(new SearchWindowContext(pos, width), provider);
            }
            if (!multi && v != null)
            {
                EditorGUI.indentLevel++;
                var property = m_Intent.Copy();
                var depth = property.depth;
                if (property.NextVisible(true))
                {
                    EditorGUI.BeginChangeCheck();
                    while (property.depth > depth)
                    {
                        EditorGUILayout.PropertyField(property, true);
                        if (!property.NextVisible(false))
                            break;
                    }
                }
                EditorGUI.indentLevel--;
            }
            changed |= EditorGUI.EndChangeCheck();
            if (changed)
                serializedObject.ApplyModifiedProperties();
        }

        private bool SelectIntent(SearchPanelIntentProvider providor, SearchTreeEntry entry, SearchWindowContext context)
        {
            var dt = entry.userData as SearchTreeLib<SearchPanelIntentProvider>.NodeInfo;
            if(/*dt != null &&*/ m_Intent != null)
            {
                var inst = dt == null ? null : dt.WrapType.Create();
                m_Intent.managedReferenceValue = inst;
                serializedObject.ApplyModifiedProperties();
                return true;
            }
            return false;
        }
    }
}
